package com.lanou.zy.projecta.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.activity.CountryActivity;
import com.lanou.zy.projecta.adapter.HotCountryRecyclerViewAdapter;
import com.lanou.zy.projecta.entity.World;
import com.lanou.zy.projecta.util.OnItemClickListener;
import com.lanou.zy.projecta.util.ProgressView.CircleProgress;
import com.lanou.zy.projecta.util.SpacesItemDecoration;

import java.io.Serializable;
import java.util.List;

/**
 * Created by dllo on 15/10/27.
 */
public class HotCountryFragment extends Fragment{
    private TextView hotCountryTitle_Tv, CountryCNNTv;
    private NetworkImageView countryImgIv;
    private List<World.DataEntity.HotCountryEntity> wdh;

    private HotCountryRecyclerViewAdapter hcRecyclerViewAdapter;
    private RecyclerView recyclerView;

    public static HotCountryFragment newInstance(List<World.DataEntity.HotCountryEntity> list, String id){
        HotCountryFragment f = new HotCountryFragment();
        Bundle args = new Bundle();
        args.putSerializable("list", (Serializable) list);
        args.putSerializable("id", id);
        f.setArguments(args);
        return f;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_hot_country, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view) {
        hotCountryTitle_Tv = (TextView) view.findViewById(R.id.hotCountryTitle_Tv);
        countryImgIv = (NetworkImageView) view.findViewById(R.id.countryImgIv);
        CountryCNNTv = (TextView) view.findViewById(R.id.CountryCNNTv);


        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView = (RecyclerView) view.findViewById(R.id.hotCountryRecyclerView);

        recyclerView.setLayoutManager(gridLayoutManager);
        hcRecyclerViewAdapter = new HotCountryRecyclerViewAdapter(getActivity());
        recyclerView.setAdapter(hcRecyclerViewAdapter);

        hcRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(), CountryActivity.class);
                intent.putExtra("id", wdh.get(position).getId() + "");
                intent.putExtra("type", wdh.get(position).getLabel() + "");
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    private void initData() {
        hotCountryTitle_Tv.setText(getArguments().getString("id") + "热门目的地");

        wdh = (List<World.DataEntity.HotCountryEntity>) getArguments().getSerializable("list");
        hcRecyclerViewAdapter.addData(wdh);
    }



}
